package com.cskaoyan.controller;

import com.cskaoyan.bean.AdminListRespVo;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.StorageListBean;
import com.cskaoyan.bean.StorageVO;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.service.StorageService;

import com.cskaoyan.service.StorageServiceBAK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;




/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/6
 * @TIME: 14:43
 * @PROJECT_NAME: mallproject2
 */
@RestController
@RequestMapping("admin/storage")
public class StorageController {
    @Autowired
    //StorageService storageService;
    StorageServiceBAK storageService;


    @RequestMapping("create")
    public AdminListRespVo<StorageVO> storage(MultipartFile file) throws IOException {
        //POST http://182.92.235.201:8083/admin/storage/create HTTP/1.1
        //文件上传
        //transferTo → 参数 MultipartFile转换为一个什么样的file

        //将文件信息保存至数据库
        StorageVO storageVO = storageService.storage(file);
        return AdminListRespVo.listOk(storageVO);
    }



    @GetMapping("list")
    public AdminListRespVo<StorageListBean> list(String name, String key, BaseParam baseParam) {
        //查询数据库
        StorageListBean storageListBean = storageService.list(name, key, baseParam);
        return AdminListRespVo.listOk(storageListBean);
    }


    @PostMapping("delete")
    public AdminListRespVo<String> delete(@RequestBody Storage storage) {
        int code = storageService.delete(storage);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return AdminListRespVo.listOk("null");
    }

    @PostMapping("update")
    public AdminListRespVo<Storage> update(@RequestBody Storage storage) {

        Storage newStorage = storageService.update(storage);

        return AdminListRespVo.listOk(newStorage);
    }


//    @Autowired
//    GoodsService goodsService;

//    @ResponseBody
//    @RequestMapping("create")
//   public Result imageUpload(MultipartFile file) throws IOException {
//        String filename = file.getOriginalFilename();
//        long size = file.getSize();
//        Date date = new Date();
//
//        String type = file.getContentType();
//
//        Storage storage = new Storage();
//
//        storage.setSize((int) size);
//        storage.setAddTime(date);
//        storage.setName(filename);
//        storage.setKey(filename);
//        storage.setUpdateTime(date);
//        storage.setType(type);
//        storage.setUrl("http://localhost:8083" + "/wx/storage/fetch/" + filename);
//        file.transferTo(new File("E:\\static\\" + filename));
//        Integer id = goodsService.insertStorage(storage);
//        storage.setId(id);
//
//        return Result.ok(storage);
//    }


}
